//LCR 008. 长度最小的子数组
// 思路：滑动窗口


#include <vector>
#include <algorithm>
using namespace std;

class Solution
{
public:
    int minSubArrayLen(int target, vector<int>& nums)
    {
        int n = nums.size(), len = INT_MAX, sum = 0;
        int left = 0, right = 0;
        while(right < n)
        {
            sum += nums[right];
            while(sum >= target)// 判断
            {
                len = min(len, right-left+1);
                sum -= nums[left++];
            }
            if(sum < target) right++;
        }
        return len == INT_MAX ? 0 : len;
    }
};